home *** CD-ROM | disk | FTP | other *** search
-
- #include <stdio.h>
- #include <dos.h>
- #include <ctype.h>
-
- #define TRUE 1
- #define FALSE 0
- #define DEFAULT "MAKEFILE"
- #define INMAX 255 /* maximum input line length */
- extern char *getmem() ;
- extern int _oserr, errno ;
-
-
- struct sym_ptr
- {
- struct sym_str *data ;
- struct sym_ptr *next ;
- } ;
-
- struct sym_str
- {
- char symbol[40] ; /* max symbol size in lattice c is 39 chars */
- char equals[81] ; /* so we had to default to something */
- } ;
-
- /* declare some global variables for the symbols */
- struct sym_ptr list ;
- struct sym_ptr *current ; /* pointer to the current symbol */
- /* thus we know where to add more to */
-
- struct howrec {
- char *howcom,*howargs;
- struct howrec *nexthow;
- };
-
- struct deprec {
- char *name;
- struct defnrec *def;
- struct deprec *nextdep;
- };
-
- struct defnrec {
- char *name;
- int uptodate;
- long modified;
- struct deprec *dependson;
- struct howrec *howto;
- struct defnrec *nextdefn;
- };
-
- struct dorec {
- char *name;
- struct dorec *nextdo;
- };
-
- struct defnrec *defnlist;
- struct dorec *dolist;
-
- int execute;
- int stopOnErr;
- int madesomething;
- int knowhow;
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- long make();
- long void ;
- current = &list ;
- current->next = NULL ;
- current->data = NULL ;
- init(argc,argv);
-
- /* now fall down the dolist and do them all */
- while (dolist != NULL) {
- madesomething = FALSE;
- void = make(dolist->name); /* ignore return value */
- if (!madesomething) {
- if (knowhow)
- fprintf(stderr,"Make: '%s' is up to date\n",dolist->name);
- else {
- fprintf(stderr,"Make: Don't know how to make '%s'\n",
- dolist->name);
- if (stopOnErr)
- exit(-1);
- }
- }
- dolist = dolist->nextdo;
- }
- }
-
- init(argc,argv)
- int argc;
- char *argv[];
- {
- int i, usedefault;
- dolist = NULL;
- defnlist = NULL;
- usedefault = TRUE;
- execute = TRUE;
- stopOnErr = TRUE;
-
- for (i=1; i < argc; i++) {
- if (argv[i][0] == '-') { /* option */
- switch (argv[i][1]) {
- case 'f': case 'F': /* arg following is a makefile */
- if (++i < argc) {
- readmakefile(argv[i]);
- usedefault = FALSE;
- } else {
- fprintf(stderr,"Make: '-f' requires filename\n");
- exit(-1);
- }
- break;
- case 'i': case 'I': /* ignore errors on execution */
- stopOnErr = FALSE;
- break;
- case 'n': case 'N': /* don't execute commands - just print */
- execute = FALSE;
- break;
- default:
- fprintf(stderr,"Make: unknown option '%s'\n",argv[i]);
- }
- } else { /* it must be something to make */
- add_do(argv[i]);
- }
- }
- if (usedefault)
- readmakefile(DEFAULT);
-
- }
-
- long make(s) /* returns the modified date/time */
- char *s;
- {
- struct defnrec *defnp;
- struct deprec *depp;
- struct howrec *howp;
- char exp_args[128] ;
- char system_call[255] ;
- long latest, getmodified(), lmax(), currtime();
- int void, return_code ;
-
- /* look for the definition */
- defnp = defnlist;
- while (defnp != NULL) {
- if (strcmp(defnp->name,s) == 0)
- break;
- defnp = defnp->nextdefn;
- }
-
- if (defnp == NULL) { /* don't know how to make it */
- knowhow = FALSE;
- latest = getmodified(s);
- if (latest==0) { /* doesn't exist but don't know how to make */
- fprintf(stderr,"Make: Can't make '%s'\n",s);
- exit(-1);
- } else /* exists - assume it's up to date since we don't know */
- return(latest);
- }
-
- if (defnp->uptodate)
- return(defnp->modified);
-
- /* now make sure everything that it depends on is up to date */
- latest = 0;
- depp = defnp->dependson;
- while (depp != NULL) {
- latest = lmax(make(depp->name),latest);
- depp = depp->nextdep;
- }
-
- knowhow = TRUE; /* has dependencies therefore we know how */
-
- /* if necessary, execute all of the commands to make it */
- /* if (out of date) || (depends on nothing) */
- if (latest > defnp->modified || defnp->dependson==NULL) {
- /* make those suckers */
- howp = defnp->howto;
- while (howp != NULL) {
- expand_args(howp->howargs, exp_args) ;
- printf("%s %s\n",howp->howcom,exp_args);
- if (execute)
- {
- void = forklp(howp->howcom,exp_args,NULL) ;
- if (void == 0) /* success loading function */
- {
- /* now do a wait to see if the function returns an ok code */
- return_code = wait() ;
- if (return_code != 0) /* error code returned */
- {
- printf("MAKE: error returned from %s %s, code = %d\n",
- howp->howcom, exp_args, return_code ) ;
- if (stopOnErr)
- exit(-1);
- } /* endif return_code != 0 */
- }
- else
- { /* fork had an error loading */
- if (_oserr == 2) /* error for file not found */
- { /* lets do a system call instead then */
- strcpy( system_call, howp->howcom ) ;
- strcat( system_call, " " ) ;
- strcat( system_call, exp_args ) ;
- void = system( system_call ) ;
- if (void != 0) /* error in system call */
- {
- printf("MAKE: system call error from %s %s, code = %d\n",
- howp->howcom, exp_args, _oserr ) ;
- if (stopOnErr)
- exit(-1);
- } /* endif id void != 0 */
- }
- else
- {
- printf("\nMake: error on '%s %s' _oserr = %d\n",
- howp->howcom,exp_args, _oserr);
- if (stopOnErr)
- exit(-1);
- } /* endif _oserr == 2 */
- } /* endif void == 0 */
- } /* endif execute */
- howp = howp->nexthow;
- } /* end while */
- defnp->modified = currtime();
- defnp->uptodate = TRUE;
- if (defnp->howto != NULL) /* we had instructions */
- madesomething = TRUE;
- }
-
- return(defnp->modified);
-
- }
-
- expand_args( in, out )
- char *in, *out ;
- {
- char *posin, *posout, *possym ;
- char symbol[40] ;
- char *get_symval(), *symval ;
-
- posin = in ;
- posout = out ;
- while (TRUE)
- {
- while ( (*posin != '$') && (*posin != '\0') )
- *posout++ = *posin++ ; /* copy till we find a $ */
-
- if (*posin == '\0') /* have reached the end of the input */
- { /* so exit */
- *posout = '\0' ; /* terminate the output string first tho */
- return ;
- }
-
- possym = symbol ; /* point symbol pointer at the begining */
- /* skip over the $ so we can test for alphanumeric */
- *possym++ = *posin++ ; /* copy's the $ */
- while ( isalnum(*posin) )
- *possym++ = *posin++ ; /* copy in the suspected symbol */
- *possym = '\0' ; /* zero terminate it */
- /* get the value of the found symbol */
- if ( (symval = get_symval( symbol )) == NULL )
- { /* do this if there isn't a symbol match */
- possym = symbol ; /* reset the symbol position */
- /* copy the symbol to the output */
- while (*possym != '\0' )
- *posout++ = *possym++ ;
- }
- else /* do this if there is one */
- {
- /* copy the symbol equate to the output */
- while (*symval != 0)
- *posout++ = *symval++ ;
- }
- }
- /* should never get to here */
- return ;
- }
-
- char *get_symval( symbol )
- char *symbol;
- {
- struct sym_ptr *cur_ptr ;
-
- cur_ptr = &list ; /* start at the begining of the list */
- while (cur_ptr->data != NULL) /* while there is data */
- {
- if (strcmp(cur_ptr->data->symbol, symbol) == 0) /* they match */
- {
- return(cur_ptr->data->equals) ; /* return the pointer to */
- /* the equate string */
- }
- else /* they dont */
- cur_ptr = cur_ptr->next ; /* point cur_ptr to the next symbol */
- }
- return(NULL) ;
- }
-
- add_do(s)
- char *s;
- {
- struct dorec *ptr1, *ptr2;
- char *get_mem();
-
- ptr1 = (struct dorec *)get_mem(sizeof(struct dorec));
-
- ptr1->name = s; /* okay since only called with an argv */
- ptr1->nextdo = NULL;
-
- uppercase(ptr1->name);
-
- /* now go down the dolist */
- if (dolist == NULL)
- dolist = ptr1;
- else {
- ptr2 = dolist;
- while (ptr2->nextdo != NULL)
- ptr2 = ptr2->nextdo;
- ptr2->nextdo = ptr1;
- }
-
- }
-
-
- readmakefile(s)
- char *s;
- {
- int doneline, pos, i, j;
- long getmodified() ;
- FILE *fil ;
- char inline[INMAX], info[INMAX];
- char *get_mem();
- struct defnrec *defnp, *defnp2;
- struct deprec *depp, *depp2;
- struct howrec *howp, *howp2;
-
- if ( (fil = fopen(s,"r")) == NULL)
- {
- fprintf(stderr,"Make: Couldn't open '%s'\n",s);
- return;
- }
-
- while (fgets(inline,INMAX,fil) != NULL)
- {
- inline[strlen(inline)-1] = '\0'; /* strip trailing newline */
-
- if (inline[0] == '\0') /* ignore blank lines */
- continue;
-
- switch (inline[0])
- {
- default:
- uppercase(inline);
-
- /* get what we're defining into info */
- if (sscanf(inline,"%s ",info) != 1)
- {
- fprintf(stderr,"Make: Can't scan: '%s'\n",inline);
- continue;
- }
- /* get a new struct */
- defnp = (struct defnrec *)get_mem(sizeof(struct defnrec));
- /* add it to the end of defnlist */
- if (defnlist == NULL)
- defnlist = defnp;
- else
- {
- defnp2 = defnlist;
- while (defnp2->nextdefn != NULL)
- defnp2 = defnp2->nextdefn;
- defnp2->nextdefn = defnp;
- }
- /* initialize it */
- defnp->name = get_mem(strlen(info)+1);
- strcpy(defnp->name,info);
- defnp->uptodate = FALSE; /* actually unknown */
- defnp->modified = getmodified(defnp->name);
- defnp->dependson = NULL;
- defnp->howto = NULL;
- defnp->nextdefn = NULL;
-
- /* now go through all of its dependecies */
- /* first move past the first name */
- pos = 0;
- while (isspace(inline[pos]))
- pos++;
- while (!isspace(inline[pos]) && inline[pos]!='\0')
- pos++;
- /* now loop through those suckers */
- doneline = FALSE;
- while (!doneline)
- {
- while (isspace(inline[pos]))
- pos++;
- if (inline[pos] == '\0')
- {
- doneline = TRUE;
- continue;
- }
- for(i = 0; !isspace(inline[pos]) && inline[pos]!='\0'; )
- info[i++] = inline[pos++];
- info[i] = '\0';
- /* get a new struct */
- depp = (struct deprec *)get_mem(sizeof(struct deprec));
- /* add it to the end of deplist */
- if (defnp->dependson == NULL)
- defnp->dependson = depp;
- else
- {
- depp2 = defnp->dependson;
- while (depp2->nextdep != NULL)
- depp2 = depp2->nextdep;
- depp2->nextdep = depp;
- }
- depp->name = get_mem(strlen(info)+1);
- strcpy(depp->name,info);
- depp->nextdep = NULL;
- }
- break ;
- case '+': /* must be a continuation line */
- uppercase(inline);
-
- /* now go through all of its dependecies */
- /* first move past the + sign */
- pos = 1;
- /* now loop through those suckers */
- doneline = FALSE;
- while (!doneline)
- {
- while (isspace(inline[pos]))
- pos++;
- if (inline[pos] == '\0')
- {
- doneline = TRUE;
- continue;
- }
- for(i = 0; !isspace(inline[pos]) && inline[pos]!='\0'; )
- info[i++] = inline[pos++];
- info[i] = '\0';
- /* get a new struct */
- depp = (struct deprec *)get_mem(sizeof(struct deprec));
- /* add it to the end of deplist */
- if (defnp->dependson == NULL)
- defnp->dependson = depp;
- else
- {
- depp2 = defnp->dependson;
- while (depp2->nextdep != NULL)
- depp2 = depp2->nextdep;
- depp2->nextdep = depp;
- }
- depp->name = get_mem(strlen(info)+1);
- strcpy(depp->name,info);
- depp->nextdep = NULL;
- }
- break ;
- case '$': /* must be a symbol definition */
- /* now split the line up into symbol and args */
- for (pos=0;isspace(inline[pos]); pos++);
- ;
- for (i=pos; !isspace(inline[i]) && inline[i]!='\0'; i++)
- ;
- /* if there is something there, allocate mem and copy */
- if (i != pos)
- {
- /* get a new struct */
- current->data = (struct sym_str *)get_mem(sizeof(struct sym_str));
- current->next = (struct sym_ptr *)get_mem(sizeof(struct sym_ptr));
- for(j=0; pos < i; )
- current->data->symbol[j++] = inline[pos++] ;
- current->data->symbol[j] = '\0';
- /* now look for any argumentative part */
- while (isspace(inline[pos]))
- pos++;
- for(i=0; inline[pos] != '\0'; )
- current->data->equals[i++] = inline[pos++] ;
- current->data->equals[i] = '\0' ;
- current = current->next ;
- current->next = NULL ;
- current->data = NULL ;
- }
- break ;
- case '\t':
- if (defnp == NULL)
- {
- fprintf(stderr,"Make: Howto line without a definition\n");
- fprintf(stderr,"Make: '%s'\n",inline);
- }
- /* now split the line up into command and args */
- for (pos=0;isspace(inline[pos]); pos++);
- ;
- for (i=pos; !isspace(inline[i]) && inline[i]!='\0'; i++)
- ;
- /* if there is something there, allocate mem and copy */
- if (i != pos)
- {
- /* get a new struct */
- howp = (struct howrec *)get_mem(sizeof(struct howrec));
- /* add it to the end of howlist */
- if (defnp->howto == NULL)
- defnp->howto = howp;
- else
- {
- howp2 = defnp->howto;
- while (howp2->nexthow != NULL)
- howp2 = howp2->nexthow;
- howp2->nexthow = howp;
- }
- /* copy command filename */
- howp->howcom = get_mem(i-pos+1);
- for(j=0; pos < i; )
- howp->howcom[j++] = inline[pos++];
- howp->howcom[j] = '\0';
- /* now look for any argumentative part */
- while (isspace(inline[pos]))
- pos++;
- howp->howargs = get_mem(strlen(inline)-pos + 1);
- for(i=0; inline[pos] != '\0'; )
- howp->howargs[i++] = inline[pos++];
- howp->howargs[i] = '\0';
- howp->nexthow = NULL;
- } /* if */
- break ;
- } /* switch */
- } /* while */
- fclose( fil ) ;
- } /* readmakefile */
-
-
- uppercase(s)
- char *s;
- {
-
- for( ; *s != '\0'; s++)
- *s = toupper(*s);
- }
-
-
- char *get_mem(size) /* different name to differentiate it */
- int size; /* from lattice C 2.11 function */
- {
- char *p;
-
- if ((p = getmem(size)) == NULL) {
- fprintf(stderr,"Make: Ran out of memory...\n");
- exit(-1);
- }
- return(p);
- }
-
-
- long getmodified(name)
- char *name;
- {
- struct dt_tm {
- int time ;
- int date ;
- } ;
-
- union vals {
- long ret_dt ;
- struct dt_tm dt ;
- } out_val ;
-
- union REGS in_regs ;
- union REGS out_regs ;
-
- union FLAGS ret_flags ;
- int file_handle ;
-
- /* get the file handle */
- in_regs.x.dx = (int)name ;
- in_regs.h.al = 0 ;
- in_regs.h.ah = 0x3d ;
- ret_flags.all_flags = intdos( &in_regs, &out_regs ) ;
- file_handle = out_regs.x.ax ;
- /* if the file doesn't exist then return 0 for date&time */
- if ((ret_flags.flags.cf == 1) && ((file_handle == 2) ||
- (file_handle == 4) ||
- (file_handle == 5) ||
- (file_handle == 12)))
- {
- return( 0 ) ;
- }
-
- /* get the date */
- in_regs.x.bx = file_handle ;
- in_regs.h.al = 0 ;
- in_regs.h.ah = 0x57 ;
- intdos( &in_regs, &out_regs ) ;
- out_val.dt.date = out_regs.x.dx ;
- out_val.dt.time = out_regs.x.cx ;
-
- /* close out the file */
- in_regs.x.bx = file_handle ;
- in_regs.h.ah = 0x3e ;
- intdos( &in_regs, &out_regs ) ;
- return( out_val.ret_dt ) ;
-
- }
-
- long currtime()
- /* return a long encoding the current date and time */
- {
- union {
- long ret_dt ;
- int dt[2] ;
- } out_val ;
-
- union REGS in_regs ;
- union REGS out_regs ;
-
- in_regs.h.al = 0 ;
- in_regs.h.ah = 0x2a ;
- intdos( &in_regs, &out_regs ) ;
- out_regs.x.cx -= 1980 ;
-
- out_val.dt[1] = (out_regs.x.cx << 9) ;
- out_val.dt[1] |= (out_regs.h.dh << 5) ;
- out_val.dt[1] |= (out_regs.h.dl ) ;
-
- in_regs.h.al = 0 ;
- in_regs.h.ah = 0x2c ;
- intdos( &in_regs, &out_regs ) ;
- out_val.dt[0] = (out_regs.h.ch << 11) ;
- out_val.dt[0] |= (out_regs.h.cl << 5) ;
- out_val.dt[0] |= (out_regs.h.dh >> 1) ;
- return( out_val.ret_dt ) ;
-
- }
-
- long lmax(a,b)
- long a,b;
- {
- return(a>b ? a : b);
- }
- /* ---------- */
-
-